package com.atguigu.Mytest;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class AirCondition2{
    private int flag = 1;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();

     public void pring5() throws InterruptedException {

         lock.lock();

         try {
             while(flag!=1){
                 condition.await();
             }
             for (int i = 0; i < 5; i++) {
                 System.out.println(Thread.currentThread().getName()+"\t"+i);
             }
             flag = 2;
             condition2.signal();
         } finally {
             lock.unlock();
         }

     }

    public void pring10() throws InterruptedException {
        lock.lock();

        try {
            while(flag!=2){
                condition2.await();
            }
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            flag = 3;
            condition3.signal();
        } finally {
            lock.unlock();
        }

    }

    public void pring15() throws InterruptedException {
        lock.lock();

        try {
            while(flag!=3){
                condition3.await();
            }
            for (int i = 0; i < 15; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            flag = 1;
            condition.signal();
        } finally {
            lock.unlock();
        }

    }





}


public class ProdOrderDemo {
    public static void main(String[] args) {
        AirCondition2 airCondition = new AirCondition2();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(200);
                    airCondition.pring5();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(300);
                    airCondition.pring10();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(400);
                    airCondition.pring15();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();

    }
}
